# 基础镜像
FROM nvidia/cuda:12.8.1-base-ubuntu22.04

# 工作目录
WORKDIR /app

# 安装依赖
RUN apt-get update && apt-get install -y software-properties-common && \
    add-apt-repository universe && \
    add-apt-repository multiverse && \
    apt-get update && \
    apt-get install -y libmysqlclient-dev build-essential wget libgl1 bzip2 ca-certificates libglib2.0-0 libxext6 libsm6 libxrender1 mercurial subversion && \
    apt-get clean && \
    apt-get autoremove -y

# 安装Miniconda
RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ./miniconda.sh && \
    chmod +x ./miniconda.sh && \
    ./miniconda.sh -b -u -p ~/miniconda3 && \
    rm -rf ./miniconda.sh

# 更新环境变量
ENV PATH="/root/miniconda3/bin:${PATH}"

# 创建Conda环境
RUN conda create -n cv -y python=3.8

# 激活Conda环境
SHELL ["conda", "run", "-n", "cv", "/bin/bash", "-c"]

# 安装Python依赖
RUN conda install -y numpy pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia && \
    conda install -y -c conda-forge dlib mysqlclient daphne

# 安装额外的pip依赖
COPY requirements_bak.txt .
RUN conda run -n cv pip install -r requirements_bak.txt

# 复制应用代码
COPY . /app

# 数据库迁移
RUN conda run -n cv python manage.py makemigrations && \
    conda run -n cv python manage.py migrate && \
    conda run -n cv python manage.py init

# 应用运行命令
CMD ["conda", "run", "-n", "cv", "daphne", "application.asgi:application", "-b", "0.0.0.0", "-p", "8000", "--verbosity", "3"]
